//
//  HWVIPTopCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/8/29.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWVIPTopCell: UITableViewCell {
    var bgImageView:UIImageView!
    var headImageView:UIImageView!
    var headFrameImageView:UIImageView!
    var headButton:UIButton!
    var nameLabel:GranibleLabel!
    var tagBgView:UIView!
    var tagImageView:UIImageView!
    var tagLabel:UILabel!
    var goToCenter:UIButton!
    var contentLabel:UILabel!
    var sharpBgImageView:UIImageView!
    var setMealImage:UIImageView!
    var collectionView:UICollectionView!
    var vipPrivilege:UIImageView!
    var model:HWNewVIPModel?{
        didSet {
            headImageView.kf.setImage(with: URL(string: model?.top.face ?? ""))
            headFrameImageView.sd_setImage(with: URL(string: model?.top.head_frame ?? ""), completed: nil)
            contentLabel.text = model?.top.text
            tagLabel.text = "VIP\(model?.top.vip ?? "")"
            collectionView.reloadData()
            
            nameLabel.text = model?.top.pet_name
            if (model?.top.color.count ?? 0) > 1 {
                var arr:[UIColor] = Array()
                for color in model?.top.color ?? [] {
                    arr.append(UIColor.colorWithHexString(hexString: color))
                }
                nameLabel.colors = arr
            }else {
                nameLabel.colors = [UIColor.colorWithHexString(hexString: model?.top.color[0] ?? "#FFFFFF"),UIColor.colorWithHexString(hexString: model?.top.color[0] ?? "#FFFFFF")]
            }
        }
    }
    var clickItem:((_:String)->())?
    var goToCenterClick:(()->())?
    class func cellWithTableView(tableView:UITableView) -> (HWVIPTopCell) {
        let reUse:String = "HWVIPTopCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWVIPTopCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWVIPTopCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        bgImageView = UIImageView()
        bgImageView.image = UIImage(named: "vipTopBg")
        bgImageView.isUserInteractionEnabled = true
        contentView.addSubview(bgImageView)
        
        headImageView = UIImageView()
        headImageView.image = UIImage(named: "AppIcon")
        headImageView.contentMode = .scaleAspectFill
        headImageView.layer.cornerRadius = 32*widthScale
        headImageView.layer.borderColor = UIColor.white.cgColor
        headImageView.layer.borderWidth = 1
        headImageView.layer.masksToBounds = true
        bgImageView.addSubview(headImageView)
        
        headFrameImageView = UIImageView()
        headFrameImageView.backgroundColor = .clear
        headFrameImageView.layer.masksToBounds = true
        headFrameImageView.contentMode = .scaleAspectFill
        contentView.addSubview(headFrameImageView)
        
        headButton = UIButton(type: .custom)
        headButton.addTarget(self, action: #selector(headButtonClick(_:)), for: .touchUpInside)
        bgImageView.addSubview(headButton)
        
        nameLabel = GranibleLabel()
        nameLabel.font = .boldSystemFont(ofSize: 20*widthScale)
        bgImageView.addSubview(nameLabel)
        
        tagBgView = UIView()
        tagBgView.layer.cornerRadius = 8*widthScale
        tagBgView.layer.borderWidth = 1*widthScale
        tagBgView.layer.borderColor = UIColor.colorWithHexString(hexString: "#1CD67E").cgColor
        tagBgView.layer.masksToBounds = true
        bgImageView.addSubview(tagBgView)
        
        tagImageView = UIImageView()
        tagImageView.image = UIImage(named: "vipTag")
        tagBgView.addSubview(tagImageView)
        
        tagLabel = HWLabel.createLabel(title: "", color: "#1CD67E", font: 9*widthScale)
        tagBgView.addSubview(tagLabel)
        
        goToCenter = UIButton(type: .custom)
        goToCenter.setTitle("前往 VIP 中心", for: .normal)
        goToCenter.titleLabel?.font = .boldSystemFont(ofSize: 11*widthScale)
        goToCenter.setTitleColor(.colorWithHexString(hexString: "#1CD67E"), for: .normal)
        goToCenter.layer.cornerRadius = 11*widthScale
        goToCenter.backgroundColor = .colorWithHexString(hexString: "#232323")
        goToCenter.addTarget(self, action: #selector(goToCenterClick(_:)), for: .touchUpInside)
        bgImageView.addSubview(goToCenter)
        
        contentLabel = HWLabel.createBoldLabel(title: "开通VIP，可享受VIP福利中心所有礼包", color: "#93908B", font: 11*widthScale)
        bgImageView.addSubview(contentLabel)
        
        sharpBgImageView = UIImageView()
        sharpBgImageView.image = UIImage(named: "vipTopSharpBg")
        contentView.addSubview(sharpBgImageView)
        
        setMealImage = UIImageView()
        setMealImage.image = UIImage(named: "vipSetMeal")
        contentView.addSubview(setMealImage)
        
        let flowL = UICollectionViewFlowLayout.init()
        flowL.minimumLineSpacing = 7*widthScale
        flowL.minimumInteritemSpacing = 7*widthScale
        flowL.scrollDirection = .horizontal
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: flowL)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .clear
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        collectionView.register(HWVIPTopCollectionCell.self, forCellWithReuseIdentifier: "HWVIPTopCollectionCell")
        contentView.addSubview(collectionView)
        
        vipPrivilege = UIImageView()
        vipPrivilege.image = UIImage(named: "vipPrivilege")
        contentView.addSubview(vipPrivilege)
        
    }
    
    @objc func goToCenterClick(_:UIButton) -> Void {
        self.goToCenterClick?()
    }
    
    func initFrame() -> Void {
        bgImageView.snp.makeConstraints { make in
            make.left.top.equalTo(18*widthScale).priority(.high)
            make.right.equalTo(-18*widthScale)
            make.height.equalTo(181*widthScale)
        }
        
        headImageView.snp.makeConstraints { make in
            make.left.top.equalTo(18*widthScale)
            make.height.width.equalTo(64*widthScale)
        }
        
        headFrameImageView.snp.makeConstraints { make in
            make.center.equalTo(headImageView)
            make.width.height.equalTo(80*widthScale)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(headImageView.snp.right).offset(18*widthScale)
            make.right.equalTo(goToCenter.snp.left)
            make.height.equalTo(27*widthScale)
            make.top.equalTo(headImageView.snp.top).offset(7*widthScale)
        }
        
        tagBgView.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(5*widthScale)
            make.height.equalTo(16*widthScale)
        }
        
        tagImageView.snp.makeConstraints { make in
            make.left.equalTo(5*widthScale)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(11*widthScale)
        }
        
        tagLabel.snp.makeConstraints { make in
            make.left.equalTo(tagImageView.snp.right).offset(2*widthScale)
            make.centerY.equalToSuperview()
            make.right.equalTo(-5*widthScale)
        }
        
        goToCenter.snp.makeConstraints { make in
            make.right.equalTo(-18*widthScale)
            make.top.equalTo(30*widthScale)
            make.height.equalTo(22*widthScale)
            make.width.equalTo(85*widthScale)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.top.equalTo(headImageView.snp.bottom).offset(19*widthScale)
            make.height.equalTo(15*widthScale)
        }
        
        sharpBgImageView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(contentLabel.snp.bottom).offset(17*widthScale)
            make.height.equalTo(54*widthScale)
        }
        
        setMealImage.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(sharpBgImageView.snp.top).offset(24*widthScale)
            make.width.equalTo(134*widthScale)
            make.height.equalTo(22*widthScale)
        }
        
        collectionView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(setMealImage.snp.bottom)
            make.height.equalTo(174*widthScale)
        }
        
        vipPrivilege.snp.makeConstraints { make in
            make.top.equalTo(collectionView.snp.bottom)
            make.centerX.equalToSuperview()
            make.width.equalTo(134*widthScale)
            make.height.equalTo(22*widthScale)
            make.bottom.equalToSuperview()
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
extension HWVIPTopCell:UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        for mod in model?.goods ?? [] {
            mod.isSelected = false
        }
        model?.goods[indexPath.row].isSelected = true
        collectionView.reloadData()
        
        clickItem?(model?.goods[indexPath.row].id ?? "")
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: -5*widthScale, left: 18*widthScale, bottom: 0, right: 18*widthScale)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 9*widthScale
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 9*widthScale
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return model?.goods.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        return CGSize(width: 107*widthScale, height: 129*widthScale)
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWVIPTopCollectionCell", for: indexPath) as! HWVIPTopCollectionCell
        
        cell.model = model?.goods[indexPath.row]
        return cell
    }
    
    
}
class HWVIPTopCollectionCell:UICollectionViewCell {
    var bgView:UIView!
    var titleLabel:UILabel!
    var priceLabel:UILabel!
    var lineView:UIView!
    var contentLabel:UILabel!
    var priceLeft:UILabel!
    var model:HWNewVIPGoodsModel? {
        didSet {
            titleLabel.text = "\(model?.day ?? "")天"
            contentLabel.text = model?.text1
            priceLabel.text = model?.money
            
            if model?.isSelected == true {
                bgView.layer.borderColor = UIColor.colorWithHexString(hexString: "#1CD67E").cgColor
            } else {
                bgView.layer.borderColor = UIColor.clear.cgColor
            }
        }
    }
    
    var bottomButtonClick:((_:String,_:String)->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func initUI() -> Void {
        bgView = UIView()
        bgView.frame = CGRect(x: 0, y: 0, width: 107*widthScale, height: 129*widthScale)
        UIColor.gradientColor(view: bgView, colors: [UIColor.colorWithHexString(hexString: "#5E5C58").cgColor,UIColor.colorWithHexString(hexString: "#404040").cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 0, y: 1))
        bgView.layer.cornerRadius = 7*widthScale
        bgView.layer.masksToBounds = true
        bgView.layer.borderWidth = 1
        contentView.addSubview(bgView)
        
        titleLabel = HWLabel.createLabel(title: "", color: "#EEEEEE", font: 13*widthScale)
        bgView.addSubview(titleLabel)
        
        priceLeft = HWLabel.createLabel(title: "￥", color: "#1CD67E", font: 11*widthScale)
        priceLeft.font = .systemFont(ofSize: 11*widthScale, weight: .semibold)
        bgView.addSubview(priceLeft)
        
        priceLabel = HWLabel.createBoldLabel(title: "", color: "#1CD67E", font: 24*widthScale)
        bgView.addSubview(priceLabel)
        
        lineView = UIView()
        lineView.frame = CGRect(x: 0, y: 0, width: 83*widthScale, height: 1*widthScale)
        bgView.addSubview(lineView)
        
        drawDashLine(lineView, strokeColor: UIColor.colorWithHexString(hexString: "#888888"), lineWidth: 1, lineLength: 1, lineSpacing: 1)

        contentLabel = HWLabel.createLabel(title: "", color: "#EEEEEE", font: 13*widthScale)
        contentLabel.numberOfLines = 2
        bgView.addSubview(contentLabel)
    }
    
    private func initFrame() -> Void {
        bgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(10*widthScale)
            make.height.equalTo(18*widthScale)
        }
        
        priceLeft.snp.makeConstraints { make in
            make.bottom.equalTo(priceLabel.snp.bottom).offset(-5*widthScale)
            make.right.equalTo(priceLabel.snp.left)
            make.height.equalTo(15*widthScale)
        }
        
        priceLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview().offset(7*widthScale)
            make.top.equalTo(titleLabel.snp.bottom).offset(3*widthScale)
            make.height.equalTo(34*widthScale)
        }
        
        lineView.snp.makeConstraints { make in
            make.top.equalTo(72*widthScale)
            make.centerX.equalToSuperview()
            make.width.equalTo(83*widthScale)
            make.height.equalTo(1*widthScale)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(15*widthScale)
            make.right.equalTo(-15*widthScale)
            make.bottom.equalTo(-13*widthScale)
            make.height.equalTo(36*widthScale)
        }

    }
    
}
extension HWVIPTopCell {
    @objc func headButtonClick(_:UIButton) -> Void {

    }
}
